<?php

namespace App\Admin\Forms\Userinfo;

use App\Admin\Models\EmployeeDetailModel;
use App\Admin\Models\EmployeeModel;
use Encore\Admin\Form\NestedForm;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;

class Optional extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '可选项';

    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {
        $data = \Request::input();
        $keyList = [
            'education_background', 'work_experience', 'membership', 'academic_experience',
            'research_project', 'award_experience', 'publication'
        ];
        if (!empty($data)) {
            foreach ($keyList as $key) {
                if (!empty($data[$key])) {
                    foreach ($data[$key] as $k => $item) {
                        if (!empty($item['_remove_']) && 1 === (int)$item['_remove_']) {
                            unset($data[$key][$k]);
                        }
                    }
                }
            }
        }
        $id   = !empty($data['employee_id']) ? (int)$data['employee_id'] : 0;
        if ($id) {
            $model = EmployeeDetailModel::where('employee_id', '=', $id)->first();
        }
        unset($data['detail_id']);
        if (empty($model)) {
            $result = EmployeeDetailModel::create($data);
        } else {
            $result = $model->update($data);
        }

        if ($result) {
            admin_toastr('保存成功');
        } else {
            admin_toastr('保存失败', 'error');
        }

        return back();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $id = $this->getEmployeeId();
        $this->html('<input type="hidden" name="employee_id" value="' . $id . '" class="employee_id">');

        // 教育经历
        $this->table('education_background', '教育经历', function (NestedForm $table) {
            $table->number('index', __('排序'))->min(1)->default(1)->placeholder('排序')->width('40px')->addElementClass('table-td-index')->rules('required');
            $table->text('start_date', __('开始日期(格式为“' . date('Y') . '”或“' . date('Y-m') . '”)'))->maxlength(7)->rules([
                'required',
                'min:4',
                'regex:/^(([0-9]{4})|(([0-9]{4})-([0-9]{2})))$/'
            ], [
                'regex' => '开始日期格式必须为“' . date('Y') . '”或“' . date('Y-m') . '”',
                'min'   => '开始日期最少为4个字符',
            ]);
            $table->text('end_date', __('结束日期(不填默认现在)'))->maxlength(7)->rules([
                'nullable',
                'min:4',
                'regex:/^(([0-9]{4})|(([0-9]{4})-([0-9]{2})))$/'
            ], [
                'regex' => '开始日期格式必须为“' . date('Y') . '”或“' . date('Y-m') . '”',
                'min'   => '开始日期最少为4个字符',
            ]);
            $table->text('educational_level_zh', __('学位名称-中文'))->rules('required');
            $table->text('educational_level_en', __('学位名称-英文'))->rules('required');
            $table->text('school_name_zh', __('学校名称-中文'))->rules('required');
            $table->text('school_name_en', __('学校名称-英文'))->rules('required');
        })->setWidth(11, 1)->required();

        // 工作经历
        $this->table('work_experience', '工作经历', function (NestedForm $table) {
            $table->number('index', __('排序'))->min(1)->default(1)->placeholder('排序')->width('40px')->addElementClass('table-td-index')->rules('required');
            $table->text('start_date', __('开始日期(格式为“' . date('Y') . '”或“' . date('Y-m') . '”)'))->maxlength(7)->rules([
                'required',
                'min:4',
                'regex:/^(([0-9]{4})|(([0-9]{4})-([0-9]{2})))$/'
            ], [
                'regex' => '开始日期格式必须为“' . date('Y') . '”或“' . date('Y-m') . '”',
                'min'   => '开始日期最少为4个字符',
            ]);
            $table->text('end_date', __('结束日期(不填默认现在)'))->maxlength(7)->rules([
                'nullable',
                'min:4',
                'regex:/^(([0-9]{4})|(([0-9]{4})-([0-9]{2})))$/'
            ], [
                'regex' => '开始日期格式必须为“' . date('Y') . '”或“' . date('Y-m') . '”',
                'min'   => '开始日期最少为4个字符',
            ]);
            $table->text('position_name_zh', __('职位名称-中文'))->rules('required');
            $table->text('position_name_en', __('职位名称-英文'))->rules('required');
            $table->text('address_name_zh', __('单位名称-中文'))->rules('required');
            $table->text('address_name_en', __('单位名称-英文'))->rules('required');
        })->setWidth(11, 1)->required();

        // 成员资格
        $this->table('membership', '成员资格', function (NestedForm $table) {
            $table->number('index', __('排序'))->min(1)->default(1)->placeholder('排序')->width('40px')->addElementClass('table-td-index')->rules('required');
            $table->text('organization_name_zh', __('组织名称-中文'))->rules('required');
            $table->text('organization_name_en', __('组织名称-英文'))->rules('required');
            $table->text('member_name_zh', __('成员名称-中文'))->rules('required');
            $table->text('member_name_en', __('成员名称-英文'))->rules('required');
        })->setWidth(11, 1)->required();

        // 学术任职经历
        $this->table('academic_experience', '学术任职经历', function (NestedForm $table) {
            $table->number('index', __('排序'))->min(1)->default(1)->placeholder('排序')->width('40px')->addElementClass('table-td-index')->rules('required');
            $table->text('start_date', __('开始日期(格式为“' . date('Y') . '”或“' . date('Y-m') . '”)'))->maxlength(7)->rules([
                'required',
                'min:4',
                'regex:/^(([0-9]{4})|(([0-9]{4})-([0-9]{2})))$/'
            ], [
                'regex' => '开始日期格式必须为“' . date('Y') . '”或“' . date('Y-m') . '”',
                'min'   => '开始日期最少为4个字符',
            ]);
            $table->text('end_date', __('结束日期(不填默认现在)'))->maxlength(7)->rules([
                'nullable',
                'min:4',
                'regex:/^(([0-9]{4})|(([0-9]{4})-([0-9]{2})))$/'
            ], [
                'regex' => '开始日期格式必须为“' . date('Y') . '”或“' . date('Y-m') . '”',
                'min'   => '开始日期最少为4个字符',
            ]);
            $table->text('position_name_zh', __('职位名称-中文'))->rules('required');
            $table->text('position_name_en', __('职位名称-英文'))->rules('required');
            $table->text('address_name_zh', __('单位名称-中文'))->rules('required');
            $table->text('address_name_en', __('单位名称-英文'))->rules('required');
        })->setWidth(11, 1)->required();

        // 科研项目
        $this->table('research_project', '科研项目', function (NestedForm $table) {
            $table->number('index', __('排序'))->min(1)->default(1)->placeholder('排序')->width('40px')->addElementClass('table-td-index')->rules('required');
            $table->text('start_date', __('开始日期(格式为“' . date('Y') . '”或“' . date('Y-m') . '”)'))->maxlength(7)->rules([
                'required',
                'min:4',
                'regex:/^(([0-9]{4})|(([0-9]{4})-([0-9]{2})))$/'
            ], [
                'regex' => '开始日期格式必须为“' . date('Y') . '”或“' . date('Y-m') . '”',
                'min'   => '开始日期最少为4个字符',
            ]);
            $table->text('end_date', __('结束日期(不填默认现在)'))->maxlength(7)->rules([
                'nullable',
                'min:4',
                'regex:/^(([0-9]{4})|(([0-9]{4})-([0-9]{2})))$/'
            ], [
                'regex' => '开始日期格式必须为“' . date('Y') . '”或“' . date('Y-m') . '”',
                'min'   => '开始日期最少为4个字符',
            ]);
            $table->text('amount', __('项目经费'))->rules('required');
            $table->text('project_name_zh', __('项目名称-中文'))->rules('required');
            $table->text('project_name_en', __('项目名称-英文'))->rules('required');
        })->setWidth(11, 1)->required();

        // 获奖经历
        $this->table('award_experience', '获奖经历', function (NestedForm $table) {
            $table->number('index', __('排序'))->min(1)->default(1)->placeholder('排序')->width('40px')->addElementClass('table-td-index')->rules('required');
            $table->text('title_zh', __('获奖描述-中文'))->rules('required');
            $table->text('title_en', __('获奖描述-英文'))->rules('required');
        })->setWidth(11, 1)->required();

        // 代表著作
        $this->table('publication', '发表著作', function (NestedForm $table) {
            $table->number('index', __('排序'))->min(1)->default(1)->placeholder('排序')->width('40px')->addElementClass('table-td-index')->rules('required');
            $table->text('title_zh', __('著作标题-中文'))->rules('required');
            $table->text('title_en', __('著作标题-英文'))->rules('required');
        })->setWidth(11, 1)->required();

        // 去掉图片的外层border
        \Encore\Admin\Admin::script(
            PHP_EOL . '$(".table-td-index").parent().parent().parent().parent().css({"width":"100px"});' . PHP_EOL
        );
    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        $id = $this->getEmployeeId();

        $data = EmployeeDetailModel::where('employee_id', '=', $id)->first();
        if (empty($data)) {
            return [];
        }

        return $data->toArray();
    }

    /**
     * 获取员工ID
     *
     * @return int
     */
    private function getEmployeeId()
    {
        $employee = EmployeeModel::select(['employee_id'])->where('user_id', '=', Admin::user()->id)->first();
        if (!$employee) {
            return 0;
        }

        $employee = $employee->toArray();
        return $employee['employee_id'];
    }
}
